package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysStore;
import com.ruoyi.system.mapper.SysStoreMapper;
import com.ruoyi.system.service.ISysStoreService;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 门店信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysStoreServiceImpl implements ISysStoreService {
    @Autowired
    private SysStoreMapper sysStoreMapper;
    @Autowired
    private SysUserServiceImpl sysUserService;

    /**
     * 查询门店信息
     *
     * @param id 门店信息主键
     * @return 门店信息
     */
    @Override
    public SysStore selectSysStoreById(String id) {
        return sysStoreMapper.selectSysStoreById(id);
    }

    /**
     * 查询门店信息列表
     *
     * @param sysStore 门店信息
     * @return 门店信息
     */
    @Override
    public List<SysStore> selectSysStoreList(SysStore sysStore) {
        return sysStoreMapper.selectSysStoreList(sysStore);
    }

    /**
     * 新增门店信息
     *
     * @param sysStore 门店信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysStore(SysStore sysStore) {
        int count = sysStoreMapper.countSysStoreByPhone(sysStore.getStoreUserPhone());

        Assert.isTrue(count == 0, "手机号已存在");

        String userId = SecurityUtils.getLoginUser().getUserId().toString();
        sysStore.setCreateTime(DateUtils.getNowDate());
        sysStore.setUpdateTime(DateUtils.getNowDate());
        sysStore.setCreateBy(userId);
        sysStore.setUpdateBy(userId);
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(104L);
        sysUser.setNickName(sysStore.getStoreName() + "店长");
        sysUser.setUserName(sysStore.getStoreUserPhone());
        sysUser.setPhonenumber(sysStore.getStoreUserPhone());
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysStore.getStorePwd()));
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setRoleId(2L);
        Long[] longs = new Long[1];
        longs[0] = 2L;
        sysUser.setRoleIds(longs);
        sysUser.setCreateBy(userId);
        sysUser.setCreateTime(new Date());
        sysUser.setUpdateBy(userId);
        sysUser.setUpdateTime(new Date());
        sysUserService.insertUser(sysUser);
        return sysStoreMapper.insertSysStore(sysStore);
    }

    /**
     * 修改门店信息
     *
     * @param sysStore 门店信息
     * @return 结果
     */
    @Override
    public int updateSysStore(SysStore sysStore) {
        sysStore.setUpdateTime(DateUtils.getNowDate());
        return sysStoreMapper.updateSysStore(sysStore);
    }

    /**
     * 批量删除门店信息
     *
     * @param ids 需要删除的门店信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreByIds(String[] ids) {
        // 添加校验，有关联禁止删除

        return sysStoreMapper.deleteSysStoreByIds(ids);
    }

    /**
     * 删除门店信息信息
     *
     * @param id 门店信息主键
     * @return 结果
     */
    @Override
    public int deleteSysStoreById(String id) {
        return sysStoreMapper.deleteSysStoreById(id);
    }
}
